

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[TOC] 参考资料ArangoDB 官方网址 ArangoDB 驱动程序是编程语言和 ArangoDB 之间的接口，它使程序员能够从本地语言程序中连接和操作 ArangoDB 部署。 从语言的角度来看，文档和数据库结构可以与数据类型及其方法集成。 一、ArangoDB Java Driver 支持的不同类型 1.1 BaseDocument根据之前的学习我们知道，ArangoDB数据库里存储的">
<meta property="og:type" content="article">
<meta property="og:title" content="ArangoDB 笔记2 AQL Java API _ AQL语法 _ 使用Java连接ArangoDB">
<meta property="og:url" content="http://example.com/2022/07/12/ArangoDB%20%E7%AC%94%E8%AE%B02%20AQL%20Java%20API%20_%20AQL%E8%AF%AD%E6%B3%95%20_%20%E4%BD%BF%E7%94%A8Java%E8%BF%9E%E6%8E%A5ArangoDB/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[TOC] 参考资料ArangoDB 官方网址 ArangoDB 驱动程序是编程语言和 ArangoDB 之间的接口，它使程序员能够从本地语言程序中连接和操作 ArangoDB 部署。 从语言的角度来看，文档和数据库结构可以与数据类型及其方法集成。 一、ArangoDB Java Driver 支持的不同类型 1.1 BaseDocument根据之前的学习我们知道，ArangoDB数据库里存储的">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/670fa1f3909c48fea6d1a23897fafcd9.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/d290db0c78ef4e27b4420893123a4332.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a48fa1c361fe4b7b8b1e4c72fe275d8a.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/39e44691ee2f45d591204d550baef807.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/94a4ddd37d8f47a48b809ce0bc74b819.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/f4e3678a74fe4dffa27202eb9d9110cf.png">
<meta property="article:published_time" content="2022-07-12T02:53:43.000Z">
<meta property="article:modified_time" content="2022-08-22T15:42:27.471Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="ArangoDB">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/670fa1f3909c48fea6d1a23897fafcd9.png">
  
  
  
  <title>ArangoDB 笔记2 AQL Java API _ AQL语法 _ 使用Java连接ArangoDB - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="ArangoDB 笔记2 AQL Java API _ AQL语法 _ 使用Java连接ArangoDB"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-07-12 10:53" pubdate>
          2022年7月12日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          14k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          120 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">ArangoDB 笔记2 AQL Java API _ AQL语法 _ 使用Java连接ArangoDB</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[TOC]</p>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><p><a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/drivers/">ArangoDB 官方网址</a></p>
<p>ArangoDB 驱动程序是编程语言和 ArangoDB 之间的接口，它使程序员能够从本地语言程序中连接和操作 ArangoDB 部署。</p>
<p>从语言的角度来看，文档和数据库结构可以与数据类型及其方法集成。</p>
<h1 id="一、ArangoDB-Java-Driver-支持的不同类型"><a href="#一、ArangoDB-Java-Driver-支持的不同类型" class="headerlink" title="一、ArangoDB Java Driver 支持的不同类型"></a>一、ArangoDB Java Driver 支持的不同类型</h1><hr>
<h2 id="1-1-BaseDocument"><a href="#1-1-BaseDocument" class="headerlink" title="1.1 BaseDocument"></a>1.1 BaseDocument</h2><p>根据之前的学习我们知道，ArangoDB数据库里存储的文档的属性和值是没有要求的，除了_id ，_key，_rev 系统属性以外，Edge集合里必须有 _from 和 _to 。<br>那么我们在使用Java操作ArangoDB时，如何解决检索到的集合列表，属性和值各不相同的问题呢？<br> 官方提供的解决：<a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/drivers/java-examples-base-document.html">查看文档</a><br>这些问题可以使用BaseDocument类来解决，该类实现代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">BaseDocument</span>&#123;<br>    String id;			<span class="hljs-comment">// _id , 文档ID</span><br>    String key;			<span class="hljs-comment">// _key, 文档的关键词</span><br>    String revision;	<span class="hljs-comment">// _rev, 版本号</span><br>    Map&lt;String, Object&gt; properties;	<span class="hljs-comment">// 非系统的任意属性</span><br>&#125;<br></code></pre></td></tr></table></figure>
<p>在 properties中，映射可包含以下类型的值：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java">Map&lt;String, Object&gt;<br>List&lt;Object&gt;<br>Boolean<br>Number<br>String<br><span class="hljs-literal">null</span><br></code></pre></td></tr></table></figure>

<p>注：Map 和 List 都包括上面列出的类型相同的对象，即支持嵌套。</p>
<p><strong>官方案例：</strong> 从myCollection集合中获取文档myDocumentKey</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java">ArangoDB.<span class="hljs-type">Builder</span> <span class="hljs-variable">arango</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArangoDB</span>.Builder().builder();<br><br>DocumentEntity&lt;BaseDocument&gt; myObject = <br>    arango<br>        .db()<br>        .collection(<span class="hljs-string">&quot;myCollection&quot;</span>)<br>        .getDocument(<span class="hljs-string">&quot;myDocumentKey&quot;</span>, BaseDocument.class);<br></code></pre></td></tr></table></figure>
<h2 id="1-2-XML"><a href="#1-2-XML" class="headerlink" title="1.2 XML"></a>1.2 XML</h2><p>从 3.1.0 版开始，<code>arangodb-java-driver</code> 支持写入、读取和查询包含 JSON 文档的原始字符串。</p>
<p>使用<a target="_blank" rel="noopener" href="http://www.jsonml.org/">JsonML</a>，您可以将 XML 字符串转换为 JSON 字符串并再次转换回 XML。</p>
<p>这里暂时没有用到，先不做扩展，可参考官方文档：<a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/drivers/java-examples-xml-data.html">官方文档</a></p>
<h2 id="1-3-Graph"><a href="#1-3-Graph" class="headerlink" title="1.3 Graph"></a>1.3 Graph</h2><p>arangodb-java-driver支持对图形的每个顶点和边集合使用 <a target="_blank" rel="noopener" href="https://arangodb.github.io/arangodb-java-driver/latest/com/arangodb/ArangoCollection.html#importDocuments-java.util.Collection-com.arangodb.model.DocumentImportOptions-">批量导入函数来导入图形数据。</a><br>这里因为暂时没有用到，先不做扩展， 可参考官方文档：<a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/drivers/java-examples-import-graph-data.html">官方文档</a></p>
<h1 id="二、AQL"><a href="#二、AQL" class="headerlink" title="二、AQL"></a>二、AQL</h1><hr>
<p>参考文档：<a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/aql/index.html">官方文档</a></p>
<p>ArangoDB 查询语言 (AQL) 可用于检索和修改存储在 ArangoDB 中的数据。<br>执行查询时候的步骤如下图：<br>![](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/img_convert/7d4bb30318f0b1ebe140e4d07bf22b27.jpeg#">https://img-blog.csdnimg.cn/img_convert/7d4bb30318f0b1ebe140e4d07bf22b27.jpeg#</a> &#x3D;60%x)</p>
<p>AQL 主要是一种声明性语言，意味着查询表达了应该实现的结果，而不是应该如何实现。除此之外，它有两个设计目标：</p>
<ul>
<li>客户端独立性，无论哪种客户端使用哪种编程语言，所有客户端的语言和语法都相同。</li>
<li>支持复杂的查询模式和 ArangoDB提供的不同数据模型</li>
</ul>
<p>AQL 的特点：</p>
<ul>
<li>类似于结构化查询语言（SQL）</li>
<li>支持读取和修改集合数据，但不支持数据定义操作，如创建和删除数据库、集合和索引</li>
<li>属于纯数据操作语言（DML），而不是数据定义语言（DDL）和数据控制语言（DCL）</li>
</ul>
<h2 id="2-1-AQL-语法"><a href="#2-1-AQL-语法" class="headerlink" title="2.1 AQL 语法"></a>2.1 AQL 语法</h2><p>参考官方文档：<a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/aql/fundamentals-syntax.html">官方文档</a></p>
<h3 id="2-1-1-查询"><a href="#2-1-1-查询" class="headerlink" title="2.1.1 查询"></a>2.1.1 查询</h3><p>AQL 查询必须返回结果（通过 <code>RETURN</code>关键字返回）或执行数据修改操作（通过关键字 <code>INSERT</code>、<code>UPDATE</code>、<code>REPLACE</code>或 <code>REMOVE</code>来指示 <code>UPSERT</code>。</p>
<p>AQL 只允许在单个查询字符中进行一个查询；因此，分号表示一个查询的结束和分隔多个查询是不允许的。</p>
<h3 id="2-1-2-空白"><a href="#2-1-2-空白" class="headerlink" title="2.1.2 空白"></a>2.1.2 空白</h3><p>AQL支持在查询文本中使用空格（空格、回车、换行和制表位）以提高其可读性。标记必须由任意数量的空格分隔。字符串或名称中的空格必须用引号括起来才能保留。</p>
<h3 id="2-1-3-注释"><a href="#2-1-3-注释" class="headerlink" title="2.1.3 注释"></a>2.1.3 注释</h3><p>AQL支持将注释嵌入查询中的任何位置。<br>单行注释：和Java一样，<code>//</code> 开头，并在行尾或查询字符串的结尾结束。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs shell">1) // 注释<br>2) RETURN 1 // 注释<br></code></pre></td></tr></table></figure>

<p>多行注释：以 <code>/*</code> 开头，以 <code>*/</code>结尾，不支持嵌套，但可以嵌入在任意位置</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs shell">1) /* 注释 */ RETURN 1<br><br>2) /* 注释1 */ RETURN /* 注释2 */ 1 <br><br>3) /* 多行注释开始<br>    ...<br>   多行注释结束 */   <br>   <br></code></pre></td></tr></table></figure>

<h3 id="2-1-4-AQL语法关键词"><a href="#2-1-4-AQL语法关键词" class="headerlink" title="2.1.4 *AQL语法关键词"></a>2.1.4 *AQL语法关键词</h3><p>AQL所支持的语法以及描述如下表：</p>
<table>
<thead>
<tr>
<th align="left">序号</th>
<th align="left">关键词</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">1</td>
<td align="left"><code>FOR</code></td>
<td align="left">遍历数组</td>
</tr>
<tr>
<td align="left">2</td>
<td align="left"><code>RETURN</code></td>
<td align="left">返回结果</td>
</tr>
<tr>
<td align="left">3</td>
<td align="left"><code>FILTER</code></td>
<td align="left">过滤结果，用于条件查询</td>
</tr>
<tr>
<td align="left">4</td>
<td align="left"><code>SEARCH</code></td>
<td align="left">查询过滤后的结果</td>
</tr>
<tr>
<td align="left">5</td>
<td align="left"><code>SORT</code></td>
<td align="left">对结果进行排序</td>
</tr>
<tr>
<td align="left">6</td>
<td align="left"><code>LIMITE</code></td>
<td align="left">对结果进行切片，分割</td>
</tr>
<tr>
<td align="left">7</td>
<td align="left"><code>LET</code></td>
<td align="left">变量赋值</td>
</tr>
<tr>
<td align="left">8</td>
<td align="left"><code>COLLECT</code></td>
<td align="left">结果分组</td>
</tr>
<tr>
<td align="left">9</td>
<td align="left"><code>WINDOW</code></td>
<td align="left">相关行的聚合</td>
</tr>
<tr>
<td align="left">10</td>
<td align="left"><code>INSERT</code></td>
<td align="left">插入新的文档</td>
</tr>
<tr>
<td align="left">11</td>
<td align="left"><code>UPDATE</code></td>
<td align="left">更新现有文档</td>
</tr>
<tr>
<td align="left">12</td>
<td align="left"><code>REPLACE</code></td>
<td align="left">替换现有的文档</td>
</tr>
<tr>
<td align="left">13</td>
<td align="left"><code>REMOVE</code></td>
<td align="left">删除现有文档</td>
</tr>
<tr>
<td align="left">14</td>
<td align="left"><code>UPSERT</code></td>
<td align="left">插入新文档或更新现有文档</td>
</tr>
<tr>
<td align="left">15</td>
<td align="left"><code>WITH</code></td>
<td align="left">集合说明</td>
</tr>
</tbody></table>
<p>上面每一个操作都支持在查询中嵌套，比如支持双重 FOR ，假如文档存储里的属性是一个嵌套列表，可轻松地实现遍历。还有其他的一些关键字可参考：<a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/aql/fundamentals-syntax.html">官方文档</a></p>
<p>关键字的特点：</p>
<ul>
<li>不区分大小写</li>
<li>支持被转义，这意味着变量里可以使用关键字表示</li>
</ul>
<h3 id="2-1-5-名称"><a href="#2-1-5-名称" class="headerlink" title="2.1.5 名称"></a>2.1.5 名称</h3><p>通常，AQL查询中包含的名称标识由：</p>
<ul>
<li>集合</li>
<li>属性</li>
<li>变量</li>
<li>函数</li>
</ul>
<p>AQL中的名称区分大小写，collections 跟 view 的名称最大长度为 256 Byte，变量名长度可超过256Byte，但不推荐。<br>关键字不得用于名称，如果要用关键字就需要使用反引号或正括号括起来，如：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">FOR</span> doc <span class="hljs-keyword">IN</span> `<span class="hljs-keyword">filter</span>`<br>  <span class="hljs-keyword">RETURN</span> doc.`sort`<br>  <br><span class="hljs-keyword">FOR</span> doc <span class="hljs-keyword">IN</span> `<span class="hljs-keyword">filter</span>`<br>  <span class="hljs-keyword">RETURN</span> f[&quot;sort&quot;]<br></code></pre></td></tr></table></figure>
<p>若名称中包含连字符减号、括号等特殊字符，则也需要转义：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">FOR</span> doc <span class="hljs-keyword">IN</span> `my<span class="hljs-operator">-</span>coll`<br>  <span class="hljs-keyword">RETURN</span> doc<br></code></pre></td></tr></table></figure>

<h4 id="1-集合名称"><a href="#1-集合名称" class="headerlink" title="1. 集合名称"></a>1. 集合名称</h4><p>集合名称可以直接在AQL语句中使用，但如果集合名和关键字相同，那么必须用双引号括起来<br>注：AQL 限制在一个AQL查询中<strong>最多使用 256 <strong>个集合，这个限制适用于所有涉及文档和边集合的</strong>总和。</strong></p>
<h4 id="2-属性名称"><a href="#2-属性名称" class="headerlink" title="2. 属性名称"></a>2. 属性名称</h4><p>当引用集合中文档的属性时，必须使用完全限定的属性名称，防止在嵌套查询中出现歧义，如：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">FOR</span> u <span class="hljs-keyword">IN</span> users<br>  <span class="hljs-keyword">FOR</span> f <span class="hljs-keyword">IN</span> friends<br>    <span class="hljs-keyword">FILTER</span> u.active <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-literal">true</span> <span class="hljs-operator">&amp;&amp;</span> f.active <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-literal">true</span> <span class="hljs-operator">&amp;&amp;</span> u.id <span class="hljs-operator">=</span><span class="hljs-operator">=</span> f.userId<br>    <span class="hljs-keyword">RETURN</span> u.name<br></code></pre></td></tr></table></figure>

<p>其中 u 和 f 必须指明。</p>
<h4 id="3-变量名"><a href="#3-变量名" class="headerlink" title="3. 变量名"></a>3. 变量名</h4><p>AQL允许用户将值分配给其他的变量，变量名不允许在查询上下文中重复，且必须与查询中使用的任何集合名称都不同。<br>官方使用例子：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">FOR</span> u <span class="hljs-keyword">IN</span> users<br>  LET friends <span class="hljs-operator">=</span> u.friends<br>  <span class="hljs-keyword">RETURN</span> &#123; &quot;name&quot; : u.name, &quot;friends&quot; : friends &#125;<br></code></pre></td></tr></table></figure>

<p>变量名中允许的字符：</p>
<ul>
<li>字母 A<del>Z 和 a</del>z</li>
<li>数字 0 ~ 9</li>
<li>下划线 _ 、美元 $</li>
</ul>
<p>变量名中的规则：</p>
<ol>
<li>变量名不能以数字开头：</li>
</ol>
<ul>
<li>反例：1name、5test</li>
<li>正例：name、test</li>
</ul>
<ol start="2">
<li>变量名若以下划线_开头，那么后面至少跟一个字母</li>
</ol>
<ul>
<li>反例：_1test、_2name</li>
<li>正例：_test、_name</li>
</ul>
<ol start="3">
<li>变量名中的美元符号 $ 只能用作变量名中的第一个字符，并且后面必须跟字母</li>
</ol>
<ul>
<li>反例：test$、$1test、te$st</li>
<li>正例：$test、$test1</li>
</ul>
<h2 id="2-2-数据类型"><a href="#2-2-数据类型" class="headerlink" title="2.2 数据类型"></a>2.2 数据类型</h2><p>AQL 支持仅由一个值组成的原始数据类型和多个值组成的符合数据类型，可用类型有：</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>数据类型</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>null</td>
<td>空值，没有值</td>
</tr>
<tr>
<td>2</td>
<td>boolean</td>
<td>true &#x2F; false</td>
</tr>
<tr>
<td>3</td>
<td>number</td>
<td>数字</td>
</tr>
<tr>
<td>4</td>
<td>string</td>
<td>UTF-8编码的文本</td>
</tr>
<tr>
<td>5</td>
<td>array &#x2F; list</td>
<td>序列，通过位置下标引用</td>
</tr>
<tr>
<td>6</td>
<td>object &#x2F; document</td>
<td>序列，通过对象的名称引用</td>
</tr>
</tbody></table>
<p>更多请参考：<a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/aql/fundamentals-data-types.html">https://www.arangodb.com/docs/stable/aql/fundamentals-data-types.html</a></p>
<h2 id="2-3-绑定参数"><a href="#2-3-绑定参数" class="headerlink" title="2.3 绑定参数"></a>2.3 绑定参数</h2><p>AQL 支持使用绑定参数，因此允许将查询文本与查询中使用的文字值分开，可有效防止SQL注入攻击。<br>语法：</p>
<ul>
<li>@，单个@表示替换查询中的值</li>
<li>@@，双个@通常表示对某个集合的绑定，第二个@则是集合参数的一部分</li>
</ul>
<p>语法规则：</p>
<ul>
<li>绑定参数名称必须以英文字母或数字开头，并且后面可以跟任何字母、数字或下划线。</li>
</ul>
<p>例如：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-operator">/</span><span class="hljs-operator">/</span> 错误的<br><span class="hljs-keyword">FILTER</span> u.name <span class="hljs-operator">=</span> &quot;@name&quot;<br><span class="hljs-keyword">FOR</span> doc <span class="hljs-keyword">IN</span> &quot;@@collection&quot; <br><br><span class="hljs-operator">/</span><span class="hljs-operator">/</span> 正确的<br><span class="hljs-keyword">FILTER</span> u.name <span class="hljs-operator">=</span> <span class="hljs-variable">@name</span><br><span class="hljs-keyword">FOR</span> doc <span class="hljs-keyword">IN</span> @<span class="hljs-variable">@collection</span><br></code></pre></td></tr></table></figure>

<p>AQL 提供了许多字符串操作相关的函数，例如连接字符串</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">FOR</span> u <span class="hljs-keyword">IN</span> users<br>  <span class="hljs-keyword">FILTER</span> u.id <span class="hljs-operator">=</span><span class="hljs-operator">=</span> CONCAT(<span class="hljs-string">&#x27;prefix, @id, &#x27;</span>suffix<span class="hljs-string">&#x27;) &amp;&amp; u.name == @name</span><br><span class="hljs-string">  RETURN u</span><br></code></pre></td></tr></table></figure>
<p>更多请参考：<a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/aql/functions-string.html">https://www.arangodb.com/docs/stable/aql/functions-string.html</a></p>
<p>绑定参数的传参：<br>在web页面中，需要在旁边的窗口里设置值：<br><img src="https://img-blog.csdnimg.cn/670fa1f3909c48fea6d1a23897fafcd9.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>在arangosh中，我们可以直接传递参数值，如：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">db._query(&#x27;FOR user IN @@collection RETURN user&#x27;, &#123;&#x27;@collection&#x27;:&#x27;students&#x27;&#125;)<br></code></pre></td></tr></table></figure>
<h2 id="2-4-嵌套属性"><a href="#2-4-嵌套属性" class="headerlink" title="2.4 嵌套属性"></a>2.4 嵌套属性</h2><p>绑定参数既可以用于点表示法，也可以用于子属性访问的方括号表示法，如：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs sql">let doc <span class="hljs-operator">=</span> &#123; foo: &#123; bar: &quot;baz&quot; &#125; &#125;<br><br><span class="hljs-operator">/</span><span class="hljs-operator">/</span>doc.<span class="hljs-variable">@attr</span>.<span class="hljs-variable">@subattr</span><br><br>print(doc.foo.bar)<br><span class="hljs-operator">/</span><span class="hljs-operator">/</span> 或者<br><br><span class="hljs-operator">/</span><span class="hljs-operator">/</span>doc[<span class="hljs-variable">@attr</span>][<span class="hljs-variable">@subattr</span>]<br><br>print(doc[<span class="hljs-string">&#x27;foo&#x27;</span>][<span class="hljs-string">&#x27;bar&#x27;</span>])<br></code></pre></td></tr></table></figure>
<h2 id="2-5-类型和值顺序以及值的比较"><a href="#2-5-类型和值顺序以及值的比较" class="headerlink" title="2.5 类型和值顺序以及值的比较"></a>2.5 类型和值顺序以及值的比较</h2><p>AQL比较数据类型时使用以下类型排序：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">null</span> <span class="hljs-operator">&lt;</span> bool <span class="hljs-operator">&lt;</span> number <span class="hljs-operator">&lt;</span> string <span class="hljs-operator">&lt;</span> <span class="hljs-keyword">array</span><span class="hljs-operator">/</span>list <span class="hljs-operator">&lt;</span> object<span class="hljs-operator">/</span>document<br></code></pre></td></tr></table></figure>
<p>这意味着 null 是 AQL中最小的类型，而 object和document是具有最高的类型。<br>如果两个比较的操作数有相同的数据类型，则比较操作数的值。对于基本类型（null、boolean、number和string），结果定义如下：</p>
<ul>
<li>null， null &#x3D;&#x3D; null</li>
<li>布尔值， false &lt; true </li>
<li>number，数值按其基值排序</li>
<li>字符串，字符串值用本地语言比较排序，</li>
</ul>
<p>注：与 SQL不同， null 可与任何值进行比较，包括 null 本身，结果不会自动转换为 null</p>
<p><strong>复合类型的比较：</strong><br>从第一个元素开始，通过逐个位置比较它们的各个元素来比较两个数组的值。对于每个位置，首先比较元素类型</p>
<ul>
<li>如果类型不相等，则判断比较结果，比较结束。</li>
<li>如果类型相等，则比较两个元素的值。</li>
<li>如果两个数组长度不同，则用 null 替代作为比较值，null比其他除null的值都小。</li>
</ul>
<p>当数组元素本身就是一个复合值（数组 &#x2F; 对象&#x2F; 文档），那么比较算法将递归检查元素的子值，递归比较元素的子元素。以下是官方提供的例子，更多说明请参考：<a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/aql/fundamentals-type-value-order.html">https://www.arangodb.com/docs/stable/aql/fundamentals-type-value-order.html</a></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs sql">[ ]  <span class="hljs-operator">&lt;</span>  [ <span class="hljs-number">0</span> ]<br>[ <span class="hljs-number">1</span> ]  <span class="hljs-operator">&lt;</span>  [ <span class="hljs-number">2</span> ]<br>[ <span class="hljs-number">1</span>, <span class="hljs-number">2</span> ]  <span class="hljs-operator">&lt;</span>  [ <span class="hljs-number">2</span> ]<br>[ <span class="hljs-number">99</span>, <span class="hljs-number">99</span> ]  <span class="hljs-operator">&lt;</span>  [ <span class="hljs-number">100</span> ]<br>[ <span class="hljs-literal">false</span> ]  <span class="hljs-operator">&lt;</span>  [ <span class="hljs-literal">true</span> ]<br>[ <span class="hljs-literal">false</span>, <span class="hljs-number">1</span> ]  <span class="hljs-operator">&lt;</span>  [ <span class="hljs-literal">false</span>, <span class="hljs-string">&#x27;&#x27;</span> ]<br><br>&#123; &#125;  <span class="hljs-operator">=</span><span class="hljs-operator">=</span>  &#123; &quot;a&quot; : <span class="hljs-keyword">null</span> &#125;<br><br>&#123; &#125;  <span class="hljs-operator">&lt;</span>  &#123; &quot;a&quot; : <span class="hljs-number">1</span> &#125;<br>&#123; &quot;a&quot; : <span class="hljs-number">1</span> &#125;  <span class="hljs-operator">&lt;</span>  &#123; &quot;a&quot; : <span class="hljs-number">2</span> &#125;<br>&#123; &quot;b&quot; : <span class="hljs-number">1</span> &#125;  <span class="hljs-operator">&lt;</span>  &#123; &quot;a&quot; : <span class="hljs-number">0</span> &#125;<br>&#123; &quot;a&quot; : &#123; &quot;c&quot; : <span class="hljs-literal">true</span> &#125; &#125;  <span class="hljs-operator">&lt;</span>  &#123; &quot;a&quot; : &#123; &quot;c&quot; : <span class="hljs-number">0</span> &#125; &#125;<br>&#123; &quot;a&quot; : &#123; &quot;c&quot; : <span class="hljs-literal">true</span>, &quot;a&quot; : <span class="hljs-number">0</span> &#125; &#125;  <span class="hljs-operator">&lt;</span>  &#123; &quot;a&quot; : &#123; &quot;c&quot; : <span class="hljs-literal">false</span>, &quot;a&quot; : <span class="hljs-number">1</span> &#125; &#125;<br><br>&#123; &quot;a&quot; : <span class="hljs-number">1</span>, &quot;b&quot; : <span class="hljs-number">2</span> &#125;  <span class="hljs-operator">=</span><span class="hljs-operator">=</span>  &#123; &quot;b&quot; : <span class="hljs-number">2</span>, &quot;a&quot; : <span class="hljs-number">1</span> &#125;<br></code></pre></td></tr></table></figure>
<h2 id="2-6-AQL-高级语句"><a href="#2-6-AQL-高级语句" class="headerlink" title="2.6 AQL 高级语句"></a>2.6 AQL 高级语句</h2><p>AQL 高级语句支持一些高级的查询，包括排序、切片之类的。<br>参考文档：<a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/aql/operations.html">https://www.arangodb.com/docs/stable/aql/operations.html</a><br>由于内容比较多，目前还没有应用的需求，暂时不拓展了，按需进行学习，这里先放个传送门。</p>
<h2 id="2-7-AQL-调用的三种方式"><a href="#2-7-AQL-调用的三种方式" class="headerlink" title="2.7 AQL 调用的三种方式"></a>2.7 AQL 调用的三种方式</h2><ul>
<li>通过 Web 页面</li>
</ul>
<p><a target="_blank" rel="noopener" href="http://localhost:8529/_db/_system/_admin/aardvark/index.html#login">http://localhost:8529/_db&#x2F;_system&#x2F;_admin&#x2F;aardvark&#x2F;index.html#login</a><br><img src="https://img-blog.csdnimg.cn/d290db0c78ef4e27b4420893123a4332.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<ul>
<li>通过 Java API</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/a48fa1c361fe4b7b8b1e4c72fe275d8a.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<ul>
<li>通过 arangosh 进行查询</li>
</ul>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/3cf02de466ec44ec8a18e9f875149204.png#">https://img-blog.csdnimg.cn/3cf02de466ec44ec8a18e9f875149204.png#</a> &#x3D;60%x)</p>
<h2 id="2-8-arangosh-执行-AQL"><a href="#2-8-arangosh-执行-AQL" class="headerlink" title="2.8 arangosh 执行 AQL"></a>2.8 arangosh 执行 AQL</h2><p>这里我们主要熟悉原生的AQL执行语句，这里大致熟悉以后，在Web页面和其他编程语言的API上就没有多大问题了。</p>
<h3 id="2-8-1-AQL-集合、文档的创建"><a href="#2-8-1-AQL-集合、文档的创建" class="headerlink" title="2.8.1 AQL 集合、文档的创建"></a>2.8.1 AQL 集合、文档的创建</h3><p>当我们在终端使用 <code>arangosh --server.username &quot;root&quot; --server.database 数据库名</code>指定数据库进行连接后，可以直接使用db对象，进行DML操作。<br>为方便之后测试，我们先创建一些测试的数据：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs shell">创建集合<br>db._create(&quot;testUser&quot;)<br><br>保存文档<br>db.testUser.save(&#123; _key: &quot;user&quot;, name: &quot;uni&quot;, password: &quot;123&quot;&#125;)<br><br></code></pre></td></tr></table></figure>
<h3 id="2-8-2-query-查询语句"><a href="#2-8-2-query-查询语句" class="headerlink" title="2.8.2 _query 查询语句"></a>2.8.2 _query 查询语句</h3><p>参考文档：<a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/aql/invocation-with-arangosh.html">https://www.arangodb.com/docs/stable/aql/invocation-with-arangosh.html</a></p>
<ol>
<li><p><strong>普通查询：</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">db._query(&#x27;FOR user IN testUser RETURN user&#x27;)<br></code></pre></td></tr></table></figure>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/caaa93a147ed4c0c92c330f42d28b692.png#">https://img-blog.csdnimg.cn/caaa93a147ed4c0c92c330f42d28b692.png#</a> &#x3D;60%x)</p>
</li>
<li><p><strong>绑定参数查询：</strong>（其中 FILTER 表示过滤，后接判断语句，用于条件查询）</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs shell">db._query(&#x27;FOR user IN @@collection FILTER user.name == @name RETURN user&#x27;,<br>&#123;<br>  &#x27;@collection&#x27;: &#x27;testUser&#x27;,<br>  &#x27;name&#x27;: &#x27;uni&#x27;<br>&#125;)<br></code></pre></td></tr></table></figure>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/7d25def53eaa456992c903c617d1dc6f.png#">https://img-blog.csdnimg.cn/7d25def53eaa456992c903c617d1dc6f.png#</a> &#x3D;60%x)</p>
</li>
<li><p><strong>ES6 模板字符串方式查询</strong></p>
</li>
</ol>
<p><strong>AQL支持用 ** aql<code>ES6模板语句</code></strong> **的形式来指定AQL语句，可以使用 ${} 表示插值语法。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell">var key = &#x27;user&#x27;<br><br>db._query(aql`FOR user IN testUser FILTER user._key == $&#123;key&#125; RETURN user`)<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/39e44691ee2f45d591204d550baef807.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>任意 JavaScript 表达式可用于使用 aql 查询，集合对象将被自动处理，例如：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">db._query(aql`FOR user IN $&#123;db.testUser&#125; RETURN user`)<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/94a4ddd37d8f47a48b809ce0bc74b819.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>注：数据修改的 AQL 语句查询通常不返回结果（除非包含 RETURN 语句），不适用 RETURN 语句时， toArray 方法将返回一个空数组</p>
<h3 id="2-8-3-getExtra统计数据和额外信息"><a href="#2-8-3-getExtra统计数据和额外信息" class="headerlink" title="2.8.3 getExtra统计数据和额外信息"></a>2.8.3 getExtra统计数据和额外信息</h3><p><a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/aql/execution-and-performance-query-statistics.html">官方文档</a></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">db._query(aql`FOR user IN $&#123;db.testUser&#125; RETURN user`).getExtra()<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/f4e3678a74fe4dffa27202eb9d9110cf.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>统计属性的含义如下：</p>
<ul>
<li>warnings：警告信息</li>
<li>writesExecuted：成功执行的数据修改操作的总数</li>
<li>writesIgnored：不成功但由于查询选项而被忽略的数据修改操作的总数</li>
<li>scanFull：扫描没有索引的集合时迭代的文档总数。</li>
<li>scannedIndex：使用索引扫描集合时迭代的文档总数。</li>
<li>filtered：执行过滤条件后删除的文档总数。</li>
<li>httpRequests：发生的HTTP请求数</li>
<li>executionTime：执行完语句所花费的时间</li>
<li>peakMemoryUsage：查询运行时的最大内存使用量。</li>
</ul>
<p>更多可参考：<a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/aql/execution-and-performance-query-statistics.html">https://www.arangodb.com/docs/stable/aql/execution-and-performance-query-statistics.html</a></p>
<h1 id="三、Java连接ArangoDB-案例"><a href="#三、Java连接ArangoDB-案例" class="headerlink" title="三、Java连接ArangoDB 案例"></a>三、Java连接ArangoDB 案例</h1><hr>
<p>参考文档：<a target="_blank" rel="noopener" href="https://www.arangodb.com/docs/stable/drivers/java-tutorial.html">https://www.arangodb.com/docs/stable/drivers/java-tutorial.html</a><br>要使用 ArangoDB Java 驱动程序，需要将 2 个库导入到项目中：</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://github.com/arangodb/arangodb-java-driver">arangodb-java-driver</a> : 驱动程序本身</li>
<li><a target="_blank" rel="noopener" href="https://github.com/arangodb/jackson-dataformat-velocypack">jackson-dataformat-velocypack</a>：一种数据格式后端实现，支持对<a target="_blank" rel="noopener" href="https://github.com/FasterXML/jackson-databind">Jackson Databind API</a>的 VelocyPack 支持。</li>
</ul>
<h2 id="3-1-引入依赖"><a href="#3-1-引入依赖" class="headerlink" title="3.1 引入依赖"></a>3.1 引入依赖</h2><p>使用Maven引入依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.arangodb<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>arangodb-java-driver<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>6.17.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!-- json 处理 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.arangodb<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>jackson-dataformat-velocypack<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.0.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!-- slf4j 日志 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.slf4j<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>slf4j-simple<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.7.25<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>compile<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>
<h2 id="3-2-测试数据库连接"><a href="#3-2-测试数据库连接" class="headerlink" title="3.2 测试数据库连接"></a>3.2 测试数据库连接</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> dao;<br><br><span class="hljs-keyword">import</span> com.arangodb.ArangoDB;<br><span class="hljs-keyword">import</span> com.arangodb.mapping.ArangoJack;<br><br><span class="hljs-keyword">import</span> java.util.Collection;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ArangoDAO</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ArangoDB connection;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">String</span> <span class="hljs-variable">USERNAME</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;root&quot;</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">String</span> <span class="hljs-variable">PASSWORD</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;woaini&quot;</span>;<br>    <span class="hljs-keyword">static</span> &#123;<br>        connection = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArangoDB</span>.Builder()<br>                    .serializer(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArangoJack</span>())<br>                    .user(USERNAME)<br>                    .password(PASSWORD)<br>                    .build();<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Collection&lt;String&gt; <span class="hljs-title function_">showDbs</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> connection.getDatabases();<br>    &#125; <br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        showDbs().forEach(System.out::println);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>运行结果：<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/d5f6e13e815841aba8506970de5df733.png#">https://img-blog.csdnimg.cn/d5f6e13e815841aba8506970de5df733.png#</a> &#x3D;50%x)</p>
<h2 id="3-3-创建数据库"><a href="#3-3-创建数据库" class="headerlink" title="3.3 创建数据库"></a>3.3 创建数据库</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 创建数据库</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">createDb</span><span class="hljs-params">(String dbName)</span>&#123;<br>    <span class="hljs-type">ArangoDatabase</span> <span class="hljs-variable">db</span> <span class="hljs-operator">=</span> connection.db(DbName.of(dbName));<br>    <span class="hljs-keyword">return</span> db.create();<br>&#125;<br><br></code></pre></td></tr></table></figure>
<h2 id="3-4-创建集合"><a href="#3-4-创建集合" class="headerlink" title="3.4 创建集合"></a>3.4 创建集合</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> CollectionEntity <span class="hljs-title function_">createCollections</span><span class="hljs-params">(ArangoDatabase db, String name)</span>&#123;<br>    <span class="hljs-type">ArangoCollection</span> <span class="hljs-variable">collection</span> <span class="hljs-operator">=</span> db.collection(name);<br>    <span class="hljs-keyword">return</span> collection.create();<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="3-5-创建文档"><a href="#3-5-创建文档" class="headerlink" title="3.5 创建文档"></a>3.5 创建文档</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 根据BaseDocument创建文档</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> DocumentCreateEntity&lt;BaseDocument&gt; <span class="hljs-title function_">createDoc</span><span class="hljs-params">(ArangoCollection collection, BaseDocument doc)</span>&#123;<br>    <span class="hljs-keyword">return</span> collection.insertDocument(doc);<br>&#125;<br><span class="hljs-comment">// 根据Map创建文档</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> DocumentCreateEntity&lt;BaseDocument&gt; <span class="hljs-title function_">createDoc</span><span class="hljs-params">(ArangoCollection collection, Map&lt;String, Object&gt; dataMap)</span>&#123;<br>    <span class="hljs-type">BaseDocument</span> <span class="hljs-variable">doc</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BaseDocument</span>();<br>    <span class="hljs-keyword">for</span> (Map.Entry&lt;String, Object&gt; entry : dataMap.entrySet()) &#123;<br>        doc.addAttribute(entry.getKey(), entry.getValue());<br>    &#125;<br>    <span class="hljs-keyword">return</span> createDoc(collection, doc);<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="3-6-查询文档"><a href="#3-6-查询文档" class="headerlink" title="3.6 查询文档"></a>3.6 查询文档</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 查询文档: 返回 BaseDocument 对象</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> BaseDocument <span class="hljs-title function_">getDocument</span><span class="hljs-params">(ArangoCollection collection, String key)</span>&#123;<br>    <span class="hljs-type">BaseDocument</span> <span class="hljs-variable">document</span> <span class="hljs-operator">=</span> collection.getDocument(key, BaseDocument.class);<br>    <span class="hljs-keyword">return</span> document;<br>&#125;<br><span class="hljs-comment">// 查询文档: 返回非系统属性的键值对Map</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Map&lt;String, Object&gt; <span class="hljs-title function_">getDocumentPropMap</span><span class="hljs-params">(ArangoCollection collection, String key)</span>&#123;<br>    <span class="hljs-type">BaseDocument</span> <span class="hljs-variable">document</span> <span class="hljs-operator">=</span> getDocument(collection, key);<br>    <span class="hljs-keyword">return</span> document.getProperties();<br>&#125;<br><span class="hljs-comment">// 查询文档: 返回包括系统属性_id, _key, _rev在内的所有属性键值对Map</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Map&lt;String, Object&gt; <span class="hljs-title function_">getDocumentAllMap</span><span class="hljs-params">(ArangoCollection collection, String key)</span>&#123;<br>    <span class="hljs-type">BaseDocument</span> <span class="hljs-variable">document</span> <span class="hljs-operator">=</span> getDocument(collection, key);<br>    Map&lt;String, Object&gt; map = document.getProperties();<br>    map.put(<span class="hljs-string">&quot;_id&quot;</span>, document.getId());<br>    map.put(<span class="hljs-string">&quot;_key&quot;</span>, document.getKey());<br>    map.put(<span class="hljs-string">&quot;_rev&quot;</span>, document.getRevision());<br>    <span class="hljs-keyword">return</span> map;<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="3-7-综合测试"><a href="#3-7-综合测试" class="headerlink" title="3.7 综合测试"></a>3.7 综合测试</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>       <span class="hljs-type">ArangoDatabase</span> <span class="hljs-variable">db</span> <span class="hljs-operator">=</span> connection.db();<br>       System.out.println(<span class="hljs-string">&quot;【操作1】查询所有数据库: &quot;</span>);<br>       showDbs().forEach(System.out::println);<br>       System.out.println(<span class="hljs-string">&quot;【操作2】插入数据库:&quot;</span>);<br>       <span class="hljs-comment">// 创建数据库</span><br>       <span class="hljs-keyword">if</span>(createDb(<span class="hljs-string">&quot;MyDB&quot;</span>))&#123;<br>           System.out.println(<span class="hljs-string">&quot;【操作2 结果】创建成功, 查询数据库, 结果为: &quot;</span>);<br>           showDbs().forEach(System.out::println);<br>       &#125; <span class="hljs-keyword">else</span><br>           System.out.println(<span class="hljs-string">&quot;【操作2 结果】创建失败&quot;</span>);<br><br>       System.out.println(<span class="hljs-string">&quot;【操作3】创建集合&quot;</span>);<br>       <span class="hljs-type">CollectionEntity</span> <span class="hljs-variable">studentCollection</span> <span class="hljs-operator">=</span> createCollections(db, <span class="hljs-string">&quot;students&quot;</span>);<br>       <span class="hljs-type">ArangoCollection</span> <span class="hljs-variable">collection</span> <span class="hljs-operator">=</span> db.collection(<span class="hljs-string">&quot;students&quot;</span>);<br>       System.out.println(<span class="hljs-string">&quot;【操作4】查询操作3创建集合对象的ID&quot;</span>);<br>       System.out.println(studentCollection.getId());<br><br>       System.out.println(<span class="hljs-string">&quot;【操作5】创建文档&quot;</span>);<br>       <span class="hljs-type">BaseDocument</span> <span class="hljs-variable">studentDoc</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BaseDocument</span>();<br>       Map&lt;String, Object&gt; map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>();<br>       map.put(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;uni&quot;</span>);<br>       map.put(<span class="hljs-string">&quot;age&quot;</span>, <span class="hljs-number">22</span>);<br>       studentDoc.setProperties(map);<br>       studentDoc.setKey(<span class="hljs-string">&quot;uni&quot;</span>);<br>       DocumentCreateEntity&lt;BaseDocument&gt; doc = createDoc(collection, studentDoc);<br>       System.out.println(<span class="hljs-string">&quot;【操作6】查询操作创建文档的内容:&quot;</span>);<br>       Map&lt;String, Object&gt; documentAllMap = getDocumentAllMap(collection, <span class="hljs-string">&quot;uni&quot;</span>);<br>       <span class="hljs-keyword">for</span> (Map.Entry&lt;String, Object&gt; entry : documentAllMap.entrySet()) &#123;<br>           System.out.println(<span class="hljs-string">&quot;key=&quot;</span> + entry.getKey() + <span class="hljs-string">&quot;, value=&quot;</span> + entry.getValue());<br>       &#125;<br>   &#125;<br></code></pre></td></tr></table></figure>
<p>测试结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs bash">【操作1】查询所有数据库: <br>_system<br>【操作2】插入数据库:<br>【操作2 结果】创建成功, 查询数据库, 结果为: <br>MyDB<br>_system<br>【操作3】创建集合<br>【操作4】查询操作3创建集合对象的ID<br>7513<br>【操作5】创建文档<br>【操作6】查询操作创建文档的内容:<br>key=name, value=uni<br>key=age, value=22<br>key=_id, value=students/uni<br>key=_key, value=uni<br>key=_rev, value=_ecut1CO---<br></code></pre></td></tr></table></figure>
<h2 id="3-8-使用-Jackson-JsonNode-查询文档"><a href="#3-8-使用-Jackson-JsonNode-查询文档" class="headerlink" title="3.8 使用 Jackson JsonNode 查询文档"></a>3.8 使用 Jackson JsonNode 查询文档</h2><p>基于之前插入的数据，我们现在对之前插入的文档进行查询（这里未依赖之前自定义的方法）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> dao;<br><br><span class="hljs-keyword">import</span> com.arangodb.ArangoCollection;<br><span class="hljs-keyword">import</span> com.arangodb.ArangoDB;<br><span class="hljs-keyword">import</span> com.arangodb.ArangoDatabase;<br><span class="hljs-keyword">import</span> com.arangodb.mapping.ArangoJack;<br><span class="hljs-keyword">import</span> com.fasterxml.jackson.databind.JsonNode;<br><span class="hljs-keyword">import</span> com.fasterxml.jackson.databind.node.ObjectNode;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ArangoJsonDAO</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ArangoDB connection;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">String</span> <span class="hljs-variable">USERNAME</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;root&quot;</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">String</span> <span class="hljs-variable">PASSWORD</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;woaini&quot;</span>;<br>    <span class="hljs-keyword">static</span> &#123;<br>        connection = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArangoDB</span>.Builder()<br>                .serializer(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArangoJack</span>())<br>                .user(USERNAME)<br>                .password(PASSWORD)<br>                .build();<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">ArangoDatabase</span> <span class="hljs-variable">db</span> <span class="hljs-operator">=</span> connection.db();<br>        <span class="hljs-type">ArangoCollection</span> <span class="hljs-variable">collection</span> <span class="hljs-operator">=</span> db.collection(<span class="hljs-string">&quot;students&quot;</span>);<br>        <span class="hljs-type">JsonNode</span> <span class="hljs-variable">uniNode</span> <span class="hljs-operator">=</span> collection.getDocument(<span class="hljs-string">&quot;uni&quot;</span>, ObjectNode.class);<br>        System.out.println(uniNode);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>运行结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">&#123;<span class="hljs-string">&quot;_key&quot;</span>:<span class="hljs-string">&quot;uni&quot;</span>,<span class="hljs-string">&quot;_id&quot;</span>:<span class="hljs-string">&quot;students/uni&quot;</span>,<span class="hljs-string">&quot;_rev&quot;</span>:<span class="hljs-string">&quot;_ecut1CO---&quot;</span>,<span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;uni&quot;</span>,<span class="hljs-string">&quot;age&quot;</span>:22&#125;<br></code></pre></td></tr></table></figure>
<h2 id="3-9-使用-AQL语句-查询文档"><a href="#3-9-使用-AQL语句-查询文档" class="headerlink" title="3.9 使用 AQL语句 查询文档"></a>3.9 使用 AQL语句 查询文档</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">ArangoDatabase</span> <span class="hljs-variable">db</span> <span class="hljs-operator">=</span> connection.db();<br><span class="hljs-type">String</span> <span class="hljs-variable">aql</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;FOR stu in students FILTER stu.name == @name RETURN stu&quot;</span>;<br>Map&lt;String, Object&gt; map = Collections.singletonMap(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;uni&quot;</span>);<br>ArangoCursor&lt;BaseDocument&gt; cursor = db.query(aql, map, BaseDocument.class);<br><span class="hljs-comment">// 根据游标输出文档信息</span><br>cursor.forEach((doc) -&gt; &#123;<br>    System.out.println(<span class="hljs-string">&quot;文档的key=&quot;</span> + doc.getKey());<br>&#125;);<br></code></pre></td></tr></table></figure>
<p>注：</p>
<ul>
<li>AQL 查询使用@name必须绑定到值的占位符</li>
<li>query()执行定义的查询并返回ArangoCursor给定的类（这里为BaseDocument类型</li>
<li>查询结果是无序的</li>
</ul>
<h2 id="3-10-使用-AQL-语句-删除文档"><a href="#3-10-使用-AQL-语句-删除文档" class="headerlink" title="3.10 使用 AQL 语句 删除文档"></a>3.10 使用 AQL 语句 删除文档</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">ArangoDatabase</span> <span class="hljs-variable">db</span> <span class="hljs-operator">=</span> connection.db();<br><span class="hljs-type">String</span> <span class="hljs-variable">aql</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;FOR stu IN students FILTER stu.name == @name &quot;</span> +<br>    <span class="hljs-string">&quot;REMOVE stu IN students LET removed = OLD RETURN removed&quot;</span>;<br>Map&lt;String, Object&gt; map = Collections.singletonMap(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;uni&quot;</span>);<br>ArangoCursor&lt;BaseDocument&gt; cursor = db.query(aql, map, <span class="hljs-literal">null</span>, BaseDocument.class);<br>cursor.forEach((doc) -&gt; &#123;<br>    System.out.println(<span class="hljs-string">&quot;已删除的文档: &quot;</span> + doc.getKey());<br>&#125;);<br></code></pre></td></tr></table></figure>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" class="category-chain-item">数据库</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/ArangoDB/">#ArangoDB</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>ArangoDB 笔记2 AQL Java API _ AQL语法 _ 使用Java连接ArangoDB</div>
      <div>http://example.com/2022/07/12/ArangoDB 笔记2 AQL Java API _ AQL语法 _ 使用Java连接ArangoDB/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年7月12日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/07/13/ArangoDB%20%E7%AC%94%E8%AE%B01%20%E7%AE%80%E4%BB%8B%20_%20ArangoDB%20%E6%95%B0%E6%8D%AE%E6%A8%A1%E5%9E%8B%E5%92%8C%E6%A6%82%E5%BF%B5%20_%20ArangoDB%E5%9C%A8Windows%E4%B8%8B%E7%9A%84%E5%AE%89%E8%A3%85%E4%B8%8E%E4%BD%BF%E7%94%A8%20_%20Web%E7%95%8C%E9%9D%A2%E7%9A%84%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5/" title="ArangoDB 笔记1 简介 _ ArangoDB 数据模型和概念 _ ArangoDB在Windows下的安装与使用 _ Web界面的增删改查">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">ArangoDB 笔记1 简介 _ ArangoDB 数据模型和概念 _ ArangoDB在Windows下的安装与使用 _ Web界面的增删改查</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/07/11/Java%20%E7%AC%94%E8%AE%B0%20%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%B8%83%E5%A4%A7%E8%AE%BE%E8%AE%A1%E5%8E%9F%E5%88%99/" title="Java 笔记 面向对象的七大设计原则">
                        <span class="hidden-mobile">Java 笔记 面向对象的七大设计原则</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
